<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../common/taglibs.jsp"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>${table.comment!}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${r'${ctxLayui}'}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${r'${ctxLayui}'}/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${r'${ctxLayui}'}/layuiadmin/style/myStyle.css" media="all">
</head>
<body>

<div class="layui-fluid" style="padding:0;margin:0;">
    <div class="layui-card">

        <div class="layui-row layui-form" >
        <#assign fields = table.fields />
        <#list fields as field>
        <#assign comment=field.comment/>
        <#assign propertyName=field.propertyName/>
        <#assign propertyType=field.propertyType/>
            <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">${comment}</label>
                <div class="layui-input-block">
                <#if propertyType=='LocalDateTime'>
                    <input type="text" class="layui-input" id="${propertyName}-laydate-range" autocomplete="off" placeholder=" - ">
                    <input type="hidden" name="${propertyName}Start">
                    <input type="hidden" name="${propertyName}End">
                <#elseif propertyType=='Integer'>
                    <select name="${propertyName}"  id="${propertyName}" >
                    </select>
                <#else>
                    <input class="layui-input" name="${propertyName}" autocomplete="off">
                </#if>
                </div>
            </div>
        </#list>
            <div class="layui-col-xs6 layui-col-sm3 layui-col-md3"  >
                <button class="layui-btn" lay-submit lay-filter="${table.entityPath}Table-search" style="margin-left: 5px;">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="${table.entityPath}Table-refresh">
                    <i class="layui-icon layui-icon-refresh-3 layuiadmin-button-btn"></i>
                </button>
            </div>
        </div>
        <div class="layui-row" style="width: 100%;overflow-x: auto;">
            <table class="layui-hide" id="${table.entityPath}Table" lay-filter="${table.entityPath}Table"></table>
        </div>
        <script type="text/html" id="${table.entityPath}Table-toolbar">
            <div class="layui-btn-container">
                <shiro:hasPermission name="${table.entityPath}:add">
                    <button class="layui-btn layui-btn-sm" data-type="addRec">添加</button>
                </shiro:hasPermission>
                <shiro:hasPermission name="${table.entityPath}:del">
                    <button class="layui-btn layui-btn-sm layui-btn-danger" data-type="delRec">删除</button>
                </shiro:hasPermission>
            </div>
        </script>

        <script type="text/html" id="${table.entityPath}Table-bar">
            <shiro:hasPermission name="${table.entityPath}:edit">
                <a class="layui-btn layui-btn-xs" lay-event="editRec">编辑</a>
            </shiro:hasPermission>
        </script>

    </div>
</div>

<script src="${r'${ctxLayui}'}/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${r'${ctxLayui}'}/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table','dict','laydate','util'], function(){
        var $ = layui.$,table = layui.table,form = layui.form;
        var dict = layui.dict;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var util = layui.util;
    <#list fields as field>
     <#assign comment=field.comment/>
     <#assign propertyName=field.propertyName/>
     <#assign propertyType=field.propertyType/>
     <#if propertyType=='Integer'>
        dict.setSelect("${propertyName}","dict_${table.entityPath}_${propertyName}");
     </#if>
    </#list>
        <#list fields as field>
        <#assign comment=field.comment/>
            <#assign propertyName=field.propertyName/>
            <#assign propertyType=field.propertyType/>
            <#if propertyType=='LocalDateTime'>
        admin.renderDateForSearch("${propertyName}");
    </#if>
        </#list>
        <#list fields as field>
        <#assign comment=field.comment/>
            <#assign propertyName=field.propertyName/>
            <#assign propertyType=field.propertyType/>
            <#if propertyType=='Integer'>
        var ${propertyName}Tpl = function(d){
            var html = dict.showName("dict_${table.entityPath}_${propertyName}",d.${propertyName});
            return html;
        };
    </#if>
        </#list>


        <#list fields as field>
        <#assign comment=field.comment/>
            <#assign propertyName=field.propertyName/>
            <#assign propertyType=field.propertyType/>
            <#if propertyType=='LocalDateTime'>
        var ${propertyName}Tpl = function(d){
            if(d.${propertyName}){
                return util.toDateString(d.${propertyName},'yyyy-MM-dd');
            }
            return '';
        };
    </#if>
        </#list>
        var contentTpl = function (d) {
            var html = '<a style="color:#1E9FFF" href="javascript:void(0);" lay-event="showRec" >'+d.content+'</a>';
            return html;
        }

        var reset_field = {
            <#list fields as field>
        <#assign comment=field.comment/>
            <#assign propertyName=field.propertyName/>
            <#assign propertyType=field.propertyType/>
            ${propertyName}:null,
            <#if propertyType=='LocalDateTime'>
            ${propertyName}Start:null,
            ${propertyName}End:null,
            </#if>
        </#list>
           };
        var search_field = { };
        var where = {orderMap:{"createTime":"desc","id":"asc"}};
        var elemId = "${table.entityPath}Table";
        var curCtx = ctx+'/${cfg.modelName}/${table.entityPath}';
        var tableIns = table.render({
            elem:'#'+elemId
            ,url: curCtx+'/query'
            ,where:where
            ,error:admin.error
            ,cellMinWidth: 80
            ,toolbar: '#'+elemId+'-toolbar'
            ,defaultToolbar: [{
                title: '条件过滤' //标题
                ,layEvent: 'searchDiv' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-search' //图标类名
            },
                'filter', 'print', 'exports']
            ,cols: [[
                {type: 'checkbox'}
                <#list fields as field>
                <#assign comment=field.comment/>
                    <#assign propertyName=field.propertyName/>
                    <#assign propertyType=field.propertyType/>
                    <#if propertyType=='LocalDateTime' || propertyType=='Integer'>
                ,{field:'${propertyName}', title: '${comment}', align: 'center',width:130,templet:${propertyName}Tpl}
                <#else>
                ,{field:'${propertyName}', title: '${comment}', align: 'center',width:130 }
                </#if>
        </#list>
                ,{title:'操作', toolbar: '#'+elemId+'-bar', width:90,fixed:'right'}
            ]]
            ,page: true
            ,limit: 10
            ,limits: layui.setter.limits
            ,height: 'full-50'
            ,done: function(res, curr, count){
                admin.tableDone(res)
            }
        });
        var formPage = '${table.entityPath}Form.jsp';
        var showPage = '${table.entityPath}Show.jsp';
        var active = {
            showRec:function (obj) {
                window.formData = null;
                var dialog = layer.open({
                    type: 2
                    ,title: '${table.comment!}'
                    ,content: showPage
                    ,maxmin: true
                    ,area: layui.setter.area_percent
                    ,offset: 't'
                    ,btn: ['关闭']
                    ,btnAlign: 'c'
                    ,success:function(layero,index){
                        window.formData = obj.data;
                    }
                });
                layer.full(dialog)
            },
            addRec: function(){
                window.formData = {};
                var dialog = layer.open({
                    type: 2
                    ,title: '${table.comment!}'
                    ,content: formPage
                    ,maxmin: true
                    ,area: layui.setter.area_percent
                    ,offset: 't'
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#${table.entityPath}Form-submit");
                        iframeWindow.layui.form.on('submit(${table.entityPath}Form-submit)', function(data){
                            var field = data.field;
                            <#list fields as field>
                            <#assign comment=field.comment/>
                            <#assign propertyName=field.propertyName/>
                            <#assign propertyType=field.propertyType/>
                            <#if propertyType=='Boolean'>
                            field.${propertyName} = field.${propertyName}?true:false;
                            </#if>
                            </#list>
                            var rtn = admin.syncReq(curCtx+'/add',field);
                            if(rtn && rtn.code==0){
                                layer.close(index);
                                layer.msg('操作成功');
                                setTimeout(function () { active.reload() },0);
                            }else{
                                layer.msg('操作失败');
                            }
                        });
                        submit.trigger('click');
                    }
                });
                layer.full(dialog)
            },
            editRec:function(obj){
                var rec = obj.data;
                window.formData = rec;
                var dialog = layer.open({
                    type: 2
                    ,title: '${table.comment!}'
                    ,content: formPage
                    ,maxmin: true
                    ,area: layui.setter.area_percent
                    ,offset: 't'
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#${table.entityPath}Form-submit");
                        iframeWindow.layui.form.on('submit(${table.entityPath}Form-submit)', function(data){
                            var field = data.field;
                            field.id = rec.id;
                            <#list fields as field>
                            <#assign comment=field.comment/>
                            <#assign propertyName=field.propertyName/>
                            <#assign propertyType=field.propertyType/>
                            <#if propertyType=='Boolean'>
                            field.${propertyName} = field.${propertyName}?true:false;
                            </#if>
                            </#list>
                            var rtn = admin.syncReq(curCtx+'/modify',field);
                            if(rtn && rtn.code==0){
                                layer.close(index);
                                layer.msg('操作成功');
                                setTimeout(function () { active.reload(); },0);
                            }else{
                                layer.msg('操作失败');
                            }
                        });
                        submit.trigger('click');
                    }
                });
                layer.full(dialog)
            },
            delRec:function (obj) {
                var idArr = [];
                var tipMsg = '';
                if(obj){
                    idArr.push(obj.data.id);
                    tipMsg = '确定删除该记录？'
                }else{
                    var checkStatus = table.checkStatus(elemId)
                    var recs = checkStatus.data;
                    if(!recs || recs.length<=0){
                        layer.msg('请至少选择一条记录');
                        return;
                    }
                    for(var i=0;i<recs.length;i++){
                        var rec = recs[i];
                        idArr.push(rec.id);
                    }
                    tipMsg = '确定删除'+recs.length+'条记录?';
                }

                layer.confirm(tipMsg, function(index){
                    var rtn = admin.syncReq(curCtx+'/delete',{ids:idArr.join(",")});
                    if(rtn && rtn.code==0){
                        layer.msg('操作成功');
                        setTimeout(function () { active.reload() },0);
                    }else{
                        layer.msg('操作失败');
                    }
                });
            },
            reload: function(){
                tableIns.reload();
            },
            searchDiv: function(){
                <#list fields as field>
                <#assign comment=field.comment/>
                <#assign propertyName=field.propertyName/>
                <#assign propertyType=field.propertyType/>
                <#if propertyType=='Integer'>
                var ${propertyName}Arr = dict.options("dict_${table.entityPath}_${propertyName}");
                </#if>
                </#list>
                top.layui.admin.popupRight({
                    id: 'LAY_'+elemId+'PopupLayer'
                    ,area: '350px'
                    ,success: function(layero,index){
                        top.layui.view(this.id).render('${cfg.modelName}/${table.entityPath}Search',$.extend({elemId:elemId},search_field,{
                            <#list fields as field>
                            <#assign comment=field.comment/>
                            <#assign propertyName=field.propertyName/>
                            <#assign propertyType=field.propertyType/>
                            <#if propertyType=='Integer'>
                            ${propertyName}Arr:${propertyName}Arr,
                            </#if>
                            </#list>
                        })).done(
                                function () {
                                    top.layui.form.render();
                                    <#list fields as field>
                                    <#assign comment=field.comment/>
                                    <#assign propertyName=field.propertyName/>
                                    <#assign propertyType=field.propertyType/>
                                    <#if propertyType=='LocalDateTime'>
                                    admin.renderDateForSearch("${propertyName}",search_field);
                                    </#if>
                                    </#list>
                                }
                        );
                    }
                });
            },
        };
        table.on('toolbar('+elemId+')', function(obj){
            switch(obj.event){
                case 'searchDiv':
                    active.searchDiv()
                    break;
            };
        });
        table.on('tool('+elemId+')', function(obj){
            switch(obj.event){
                case 'editRec':
                    active.editRec(obj);
                    break;
                case 'showRec':
                    active.showRec(obj);
                    break;
            }
        });
        form.on('submit('+elemId+'-search)', function(data){
            var field = data.field;
            table.reload(elemId, {
                where: $.extend({},field,where)
                ,page:{curr:1}
            });
        });
        form.on('submit('+elemId+'-refresh)', function(data){
            $(':input').not(':button, :submit, :reset').val('').removeAttr('selected').removeAttr('checked');
            form.render();
            table.reload(elemId, {
                where: $.extend({},reset_field,where)
                ,page:{curr:1}
            });
        });

        top.layui.form.on('submit('+elemId+'-search)', function(data){
            var field = data.field;
            table.reload(elemId, {
                where: $.extend({},field,where)
                ,page:{curr:1}
            });
            search_field = field;
            top.layui.layer.close(top.layui.admin.popup.index);
        });

        top.layui.form.on('submit('+elemId+'-refresh)', function(data){
            top.layui.$(':input').not(':button, :submit, :reset').val('').removeAttr('selected').removeAttr('checked')
            search_field = {};
            top.layui.form.render();
            table.reload(elemId, {
                where: $.extend({},reset_field,where)
                ,page:{curr:1}
            });
        });

        $("body").on('click','.layui-btn-container .layui-btn', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        admin.tableRowCheckToggle();

    });
</script>
</body>
</html>
